﻿
// MesTestDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MesTest.h"
#include "MesTestDlg.h"
#include "afxdialogex.h"
#include "DHInterFace.h"
#include "DH_HalconEx.h"
#include "SCIPLC.in.h"
#include "DHWidgets.h"
#include "Define.h"
#include "ThreadOperation.h"
#include "Resource.h"
#include "CParamSetDlg.h"
#include "LockHelper.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CMesTestDlg 对话框




CMesTestDlg::CMesTestDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMesTestDlg::IDD, pParent)
	, m_EDT_AccountNum(_T(""))
	, m_EDT_PassWord(_T(""))
	, m_CHK_bUsePLC(FALSE)
	, m_CHK_bUseMes(FALSE)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	InitializeCriticalSection(&m_Log);
	InitializeCriticalSection(&m_csChange);
}

void CMesTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDT_ACCOUNT, m_EDT_AccountNum);
	DDX_Text(pDX, IDC_EDT_PASSWORD, m_EDT_PassWord);
	DDX_Control(pDX, IDC_STA_MESOK, m_sta_MESOK);
	DDX_Control(pDX, IDC_STA_MENG, m_sta_MESNG);
	DDX_Control(pDX, IDC_STA_PLCOK, m_STA_PLCOK);
	DDX_Control(pDX, IDC_STA_PLCNG, m_STA_PLCNG);
	DDX_Control(pDX, IDC_LIST_LOG, m_List_Log);
	DDX_Control(pDX, IDC_COM_PRODUCTTYPE, m_COM_ProductType);
	DDX_Check(pDX, IDC_CHECK_USEPLC, m_CHK_bUsePLC);
	DDX_Check(pDX, IDC_CHECK_USEMES, m_CHK_bUseMes);
}

BEGIN_MESSAGE_MAP(CMesTestDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_Onlie, &CMesTestDlg::OnBnClickedButtonOnlie)
	ON_BN_CLICKED(IDC_BUTTON_TowerLightsAndBuzzersClear, &CMesTestDlg::OnBnClickedButtonRightverify)
	ON_BN_CLICKED(IDC_BTN_PLCTEST, &CMesTestDlg::OnBnClickedBtnPlctest)
	ON_BN_CLICKED(IDC_BUTTON4, &CMesTestDlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON3, &CMesTestDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDOK, &CMesTestDlg::OnBnClickedOk)
	//ON_BN_CLICKED(IDCANCEL, &CMesTestDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON2, &CMesTestDlg::OnBnClickedButton2)
	
	ON_BN_CLICKED(IDC_BUTTON5, &CMesTestDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BTN_SAVEPARAM, &CMesTestDlg::OnBnClickedBtnSaveparam)
	ON_BN_CLICKED(IDC_BTN_EQUIPMENTPARAM, &CMesTestDlg::OnBnClickedBtnEquipmentparam)
	ON_BN_CLICKED(IDC_BTN_MesParam, &CMesTestDlg::OnBnClickedBtnMesparam)
END_MESSAGE_MAP()


// CMesTestDlg 消息处理程序

BOOL CMesTestDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	g_pWatch = LoadDHInterFaceDll("CrashRecord");
	if (g_pWatch)
	{
		g_pWatch->DoSomething("EXCEPTIONFILTER", NULL, NULL);
	}
	IniParam();
	InitUI();
	InitLogList();
	CreatLog();
	IniPLC();
	InitMES();
	IniStatus();
	IniThread();
	g_pMainDialog = this;//当前对话框赋值
	//m_tMesSoap.UserRightVerify();
	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CMesTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMesTestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
	
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMesTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CMesTestDlg::OnBnClickedButtonOnlie()
{
	// TODO: 在此添加控件通知处理程序代码
}


void CMesTestDlg::OnBnClickedButtonRightverify()
{
	
	CString  cstrLog;
	
	if (g_bUseMes)
	{
		cstrLog.Format("蜂鸣器/塔灯解除");
		((CMesTestDlg*)g_pMainDialog)->WriteOperateLog(cstrLog);
		((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);
		g_InfoMes.CommandCodeFlag = 2;

	}
	else
	{
		cstrLog.Format("蜂鸣器/塔灯解除,MES通讯关闭");
		((CMesTestDlg*)g_pMainDialog)->WriteOperateLog(cstrLog);
		((CMesTestDlg*)g_pMainDialog)->UpdateLogList(cstrLog);
	}
	
	
}

void CMesTestDlg::IniPLC()
{
	if (g_bUsePlc)
	{
		g_pPlc = CMesTestDlg::LoadDHInterFaceDll("SCIPLC");
		PLCINIT tInitPLC;
		tInitPLC.strProductName.Format("%s", "zhonghang");
		if (TRUE != g_pPlc->DoSomething("INITIALIZE", &tInitPLC, NULL))//INITIALIZE
		{
			CString strException;
			strException.Format("PLC初始化失败");
			//MessageBox(strException);
		}
	}
	else
	{
		g_pPlc = NULL;
	}
	
}



void CMesTestDlg::InitMES()
{
	if (g_InfoMes.bServer)
	{
		m_tMesSocketTcp.ServerConnect(g_InfoMes.IP, g_InfoMes.Port, g_InfoMes.dTimeOut);
	}
	else
	{
		m_tMesSocketTcp.ClientConnect(g_InfoMes.IP, g_InfoMes.Port, g_InfoMes.dTimeOut);
	}
	
}

void CMesTestDlg::IniParam()
{
	//PLC数据
	g_FilePathPLC.Format("%s/Config/SCIPLC/PLC.ini", GetCurrentAppPath());

	g_bUsePlc = GetPrivateProfileInt("PlcCom", "Use", 0, g_FilePathPLC);
	g_PLCAddress.TotalCount = GetPrivateProfileInt("Address", "TotalCount", -1, g_FilePathPLC);
	g_PLCAddress.NGCount = GetPrivateProfileInt("Address", "NGCount", 220, g_FilePathPLC);
	g_PLCAddress.OKBoxDequeueFlagAddr = GetPrivateProfileInt("Address", "OKBoxDequeueFlagAddr", 214, g_FilePathPLC);

	g_PLCAddress.OKCount = GetPrivateProfileInt("Address", "OKCount", 216, g_FilePathPLC);
	
	g_PLCAddress.MisMatch = GetPrivateProfileInt("Address", "MisMatch", -1, g_FilePathPLC);

	g_PLCAddress.MouldLife = GetPrivateProfileInt("Address", "MouldLife", 300, g_FilePathPLC);
	g_PLCAddress.CutterLife = GetPrivateProfileInt("Address", "CutterLife", 302, g_FilePathPLC);
	g_PLCAddress.UnreelingSpeedRadio = GetPrivateProfileInt("Address", "UnreelingSpeedRadio", 304, g_FilePathPLC);
	g_PLCAddress.DrawPullRatio = GetPrivateProfileInt("Address", "DrawPullRatio", 306, g_FilePathPLC);
	g_PLCAddress.FilmThickness = GetPrivateProfileInt("Address", "FilmThickness", 308, g_FilePathPLC);

	g_PLCAddress.AlarmCodeAddr = GetPrivateProfileInt("Address", "AlarmCodeAddr", 1000, g_FilePathPLC);
	
	
	g_PLCAddress.DeviceStatusAddr = GetPrivateProfileInt("Address", "DeviceStatusAddr", 200, g_FilePathPLC);
	g_PLCAddress.DeviceStatusReasonAddr = GetPrivateProfileInt("Address", "DeviceStatusReasonAddr", 200, g_FilePathPLC);
	g_PLCAddress.AlarmCodeFlagAddr = GetPrivateProfileInt("Address", "AlarmCodeFlagAddr", 218, g_FilePathPLC);
	g_PLCAddress.CommandDownloadAddr = GetPrivateProfileInt("Address", "CommandDownloadAddr", 202, g_FilePathPLC);

	g_PLCAddress.MouldCutSpeed = GetPrivateProfileInt("Address", "MouldCutSpeed", 204, g_FilePathPLC);
	g_PLCAddress.SetBoxQuanlity = GetPrivateProfileInt("Address", "SetBoxQuanlity",206, g_FilePathPLC);
	g_PLCAddress.FilmMouldCutSetLength = GetPrivateProfileInt("Address", "FilmMouldCutSetLength", 208, g_FilePathPLC);
	g_PLCAddress.LengthOffsetAddr = GetPrivateProfileInt("Address", "LengthOffsetAddr", 210, g_FilePathPLC);
	g_PLCAddress.DownloadParamModifyAddr = GetPrivateProfileInt("Address", "DownloadParamModifyAddr", 212, g_FilePathPLC);

	g_PLCAddress.BatchAddr = GetPrivateProfileInt("Address", "BatchAddr", 230, g_FilePathPLC);	

	g_PLCAddress.DanJiaAdd = GetPrivateProfileInt("Address", "DanJiaAdd", 902, g_FilePathPLC);
	g_PLCAddress.ClipChange = GetPrivateProfileInt("Address", "ClipChange", 902, g_FilePathPLC);
	g_PLCAddress.ClipChange1 = GetPrivateProfileInt("Address", "ClipChange1", 902, g_FilePathPLC);
	g_PLCAddress.CaiQieNum = GetPrivateProfileInt("Address", "CaiQieNum", 900, g_FilePathPLC);
	g_PLCAddress.MoQieNum = GetPrivateProfileInt("Address", "MoQieNum", 904, g_FilePathPLC);
	g_PLCAddress.MoQieSpeedSet = GetPrivateProfileInt("Address", "MoQieSpeedSet", 906, g_FilePathPLC);
	g_PLCAddress.MoQieSpeedRun = GetPrivateProfileInt("Address", "MoQieSpeedRun", 908, g_FilePathPLC);
	g_PLCAddress.FengSu1 = GetPrivateProfileInt("Address", "FengSu1", 910, g_FilePathPLC);
	g_PLCAddress.FengSu2 = GetPrivateProfileInt("Address", "FengSu2", 2002, g_FilePathPLC);
	g_PLCAddress.JiaQiangJiYaLi = GetPrivateProfileInt("Address", "JiaQiangJiYaLi", 2004, g_FilePathPLC);
	g_PLCAddress.FangJuanJuanJing = GetPrivateProfileInt("Address", "FangJuanJuanJing", 2006, g_FilePathPLC);
	g_PLCAddress.BaiBiZhangLiRun = GetPrivateProfileInt("Address", "BaiBiZhangLiRun", 2008, g_FilePathPLC);
	g_PLCAddress.BaiBiZhangLiSet = GetPrivateProfileInt("Address", "BaiBiZhangLiSet", 2010, g_FilePathPLC);
	g_PLCAddress.FangJuanZhangLiRun = GetPrivateProfileInt("Address", "FangJuanZhangLiRun", 2012, g_FilePathPLC);
	g_PLCAddress.FangJuanZhangLiSet = GetPrivateProfileInt("Address", "FangJuanZhangLiSet", 2014, g_FilePathPLC);
	g_PLCAddress.SizeLength = GetPrivateProfileInt("Address", "SizeLength", 2016, g_FilePathPLC);
	g_PLCAddress.XiaCiLength = GetPrivateProfileInt("Address", "XiaCiLength", 2018, g_FilePathPLC);
	g_PLCAddress.SizeAdd = GetPrivateProfileInt("Address", "SizeAdd", 2020, g_FilePathPLC);
	g_PLCAddress.XiaCiAdd1 = GetPrivateProfileInt("Address", "XiaCiAdd1", 2022, g_FilePathPLC);
	g_PLCAddress.XiaCiAdd2 = GetPrivateProfileInt("Address", "XiaCiAdd2", 504, g_FilePathPLC);

	g_PLCAddress.EquStop = GetPrivateProfileInt("Address", "EquStop", 5000, g_FilePathPLC);

	g_PLCAddress.FEIJIERMIN = GetPrivateProfileInt("Address", "FEIJIERMIN", 2014, g_FilePathPLC);
	g_PLCAddress.FEIJIERMAX = GetPrivateProfileInt("Address", "FEIJIERMAX", 2016, g_FilePathPLC);
	g_PLCAddress.FEIJIERTIME = GetPrivateProfileInt("Address", "FEIJIERTIME", 2018, g_FilePathPLC);
	g_PLCAddress.JIERMIN = GetPrivateProfileInt("Address", "JIERMIN", 2020, g_FilePathPLC);
	g_PLCAddress.JIERMAX = GetPrivateProfileInt("Address", "JIERMAX", 2022, g_FilePathPLC);
	g_PLCAddress.JIERTIME = GetPrivateProfileInt("Address", "JIERTIME", 504, g_FilePathPLC);

	//MES数据
	g_FilePathLog.Format("%s/Config/SCIMES/MES.ini", GetCurrentAppPath());
	g_bUseMes= GetPrivateProfileInt("MesCom", "Use", 0, g_FilePathLog);

	GetPrivateProfileString("EquipmentInfo", "IP", "10.125.1.122", g_InfoMes.IP.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathLog);
	g_InfoMes.Port = GetPrivateProfileInt("EquipmentInfo", "Port", 8080, g_FilePathLog);
	g_InfoMes.dTimeOut = GetPrivateProfileDouble("EquipmentInfo", "dTimeOut", 0.7, g_FilePathLog);
	g_InfoMes.bServer = GetPrivateProfileInt("EquipmentInfo", "bServer", 1, g_FilePathLog);
	g_InfoMes.bShiledHeartBeat = GetPrivateProfileInt("EquipmentInfo", "bShiledHeartBeat", 1, g_FilePathLog);

	GetPrivateProfileString("EquipmentInfo", "factoryNo", "F001", g_InfoMes.factoryNo.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathLog);
	GetPrivateProfileString("EquipmentInfo", "machineNo", "Fisrt001", g_InfoMes.machineNo.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathLog);
	GetPrivateProfileString("EquipmentInfo", "EquimentNo", "Fisrt001", g_InfoMes.EquimentNo.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathLog);
	g_InfoMes.nProdectType = GetPrivateProfileInt("EquipmentInfo", "ProdectType", 0, g_FilePathLog);

	GetPrivateProfileString("EquipmentInfo", "MesUserName", "MesUser01", g_InfoMes.MesUserName.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathLog);
	GetPrivateProfileString("EquipmentInfo", "MesUserPassWord", "MesUserPassWord01", g_InfoMes.MesUserPassWord.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathLog);
	GetPrivateProfileString("EquipmentInfo", "EventUser", "ZhangSan", g_InfoMes.EventUser.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathLog);
	g_InfoMes.nHeartBeatSpanTime=GetPrivateProfileInt("EquipmentInfo", "nHeartBeatSpanTime", 10, g_FilePathLog);
	g_InfoMes.nTimeJianGe= GetPrivateProfileInt("EquipmentInfo", "nTimeJianGe", 1, g_FilePathLog);
	g_InfoMes.CommandCodeFlag=0;
	g_InfoMes.CommandCode=6666;
	g_InfoMes.bSuccess =-1;
	g_InfoMes.ParamDownloadFlag = 0;
	g_InfoMes.bShowMateFeedingOrUnloadInfoFlag = 0;

	GetPrivateProfileString("EquipmentInfo", "Batch", "Batch", g_InfoMes.cstrBatch.GetBuffer(MAX_PATH), MAX_PATH, g_FilePathLog);

	//
	g_PLCData.nDevieStatus = 0;

	
	//系统数据
	g_SystemPathPLC.Format("%s/Config/System.ini", GetCurrentAppPath());
	GetPrivateProfileString("Land", "Use1", "ABC", g_Land.Use1.GetBuffer(MAX_PATH), MAX_PATH, g_SystemPathPLC);
	GetPrivateProfileString("Land", "PassWord1", "ABC", g_Land.PassWord1.GetBuffer(MAX_PATH), MAX_PATH, g_SystemPathPLC);
	GetPrivateProfileString("Land", "Use2", "ABC", g_Land.Use2.GetBuffer(MAX_PATH), MAX_PATH, g_SystemPathPLC);
	GetPrivateProfileString("Land", "PassWord2", "ABC", g_Land.PassWord2.GetBuffer(MAX_PATH), MAX_PATH, g_SystemPathPLC);
	GetPrivateProfileString("Land", "Use3", "ABC", g_Land.Use3.GetBuffer(MAX_PATH), MAX_PATH, g_SystemPathPLC);
	GetPrivateProfileString("Land", "PassWord3", "ABC", g_Land.PassWord3.GetBuffer(MAX_PATH), MAX_PATH, g_SystemPathPLC);
	GetPrivateProfileString("MESDATAADD", "path", "D:瑕疵\\Run-x64\\MES", g_cstrFilePath.GetBuffer(MAX_PATH), MAX_PATH, g_SystemPathPLC);
	g_nType = GetPrivateProfileInt("TYPE", "Type", 0, g_SystemPathPLC);
	g_nNGLENGTH = GetPrivateProfileInt("TYPE", "NGLENGTH", 0, g_SystemPathPLC);
	g_Land.PassLevel = -1;
}

void  CMesTestDlg::InitUI()
{
	SetDlgItemText(IDC_EDT_MainDlgFactoryNo, g_InfoMes.factoryNo);
	SetDlgItemText(IDC_EDT_MainDlgMachineNo, g_InfoMes.machineNo);
	m_CHK_bUseMes = g_bUseMes;
	m_CHK_bUsePLC = g_bUsePlc;

	UpdateData(FALSE);


}

DHInterface* CMesTestDlg::LoadDHInterFaceDll(CString strDllName)

{

	CString strDll;
	strDll.Format("%s\\%s.dll", GetCurrentAppPath(), strDllName);

	HINSTANCE hDllLog = ::LoadLibrary(_T(strDll));

	if (hDllLog == NULL)

	{

		CString strMessage;

		strMessage.Format("加载%s异常", strDllName);

		AfxMessageBox(strMessage);

		::FreeLibrary(hDllLog);

		return NULL;

	}

	typedef DHInterface*(*pExport)(void);

	pExport Create;

	Create = (pExport)GetProcAddress(hDllLog, "Export");

	if (Create == NULL)

	{

		CString strMessage;

		strMessage.Format("%s.dll获得函数地址报错", strDllName);

		AfxMessageBox(strMessage);

		::FreeLibrary(hDllLog);

		return NULL;

	}

	return Create();

}

BOOL CMesTestDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类	
	if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN)
	{
		if (pMsg->wParam == VK_RETURN)//屏蔽回车
		{
			return TRUE;
		}
		else if (pMsg->wParam == VK_ESCAPE)//屏蔽ESC
		{
			return TRUE;
		}
		else if (pMsg->wParam == VK_F4 && HIBYTE(::GetKeyState(VK_MENU)))//屏蔽ALT+F4
		{
			return TRUE;
		}
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}
void CMesTestDlg::OnBnClickedBtnPlctest()
{
	// TODO: 在此添加控件通知处理程序代码
	if (g_pPlc)
	g_pPlc->DoSomething("SHOW", NULL, NULL);
}


void CMesTestDlg::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	g_Land.PassLevel = 0;
	SetDlgItemText(IDC_EDT_ACCOUNT, "");
	SetDlgItemText(IDC_EDT_PASSWORD, "");
	GetDlgItem(IDC_BUTTON_TowerLightsAndBuzzersClear)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON_Onlie)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(0);
	GetDlgItem(IDC_BTN_EQUIPMENTPARAM)->EnableWindow(0);
	GetDlgItem(IDC_BTN_PLCTEST)->EnableWindow(0);
	
}


void CMesTestDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	if (m_EDT_AccountNum == g_Land.Use1 && m_EDT_PassWord == g_Land.PassWord1)
	{
		g_Land.PassLevel = 1;
		GetDlgItem(IDC_BUTTON_TowerLightsAndBuzzersClear)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON_Onlie)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON2)->EnableWindow(1);
		GetDlgItem(IDC_BTN_EQUIPMENTPARAM)->EnableWindow(1);
		GetDlgItem(IDC_BTN_PLCTEST)->EnableWindow(1);
		AfxMessageBox("登录成功");
	}
	else if(m_EDT_AccountNum == g_Land.Use2 && m_EDT_PassWord == g_Land.PassWord2)
	{
		g_Land.PassLevel = 2;
		GetDlgItem(IDC_BUTTON_TowerLightsAndBuzzersClear)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON_Onlie)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON2)->EnableWindow(1);
		GetDlgItem(IDC_BTN_EQUIPMENTPARAM)->EnableWindow(1);
		GetDlgItem(IDC_BTN_PLCTEST)->EnableWindow(1);
		
		SetDlgItemText(IDC_EDT_MainDlgFactoryNo, g_InfoMes.factoryNo);
		SetDlgItemText(IDC_EDT_machineNo, g_InfoMes.machineNo);
		//AfxMessageBox("登录成功");
	}
	else if (m_EDT_AccountNum == g_Land.Use3 && m_EDT_PassWord == g_Land.PassWord3)
	{
		g_Land.PassLevel = 3;
		GetDlgItem(IDC_BUTTON_TowerLightsAndBuzzersClear)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON_Onlie)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON2)->EnableWindow(1);
		GetDlgItem(IDC_BTN_EQUIPMENTPARAM)->EnableWindow(1);
		GetDlgItem(IDC_BTN_PLCTEST)->EnableWindow(1);
		
		SetDlgItemText(IDC_EDT_MainDlgFactoryNo, g_InfoMes.factoryNo);
		SetDlgItemText(IDC_EDT_machineNo, g_InfoMes.machineNo);
		AfxMessageBox("登录成功");
	}
	else
	{
		AfxMessageBox("登录失败");
	}
}


void CMesTestDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	if (IDCANCEL==AfxMessageBox("是否退出软件?"),MB_OKCANCEL)
	{
		CDialogEx::OnOK();
		
	}
	return;
	CDialogEx::OnOK();
}


//void CMesTestDlg::OnBnClickedCancel()
//{
//	// TODO: 在此添加控件通知处理程序代码
//	CDialogEx::OnCancel();
//}

void CMesTestDlg::IniStatus()
{
	Font.CreateFontA(25, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS
		, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "华文雅黑");
	GetDlgItem(IDC_STA_MESOK)->SetFont(&Font);
	GetDlgItem(IDC_STA_MENG)->SetFont(&Font);
	GetDlgItem(IDC_STA_PLCOK)->SetFont(&Font);
	GetDlgItem(IDC_STA_PLCNG)->SetFont(&Font);
	SetDlgItemText(IDC_EDT_MainDlgFactoryNo, g_InfoMes.factoryNo);
	SetDlgItemText(IDC_EDT_machineNo, g_InfoMes.machineNo);
	GetDlgItem(IDC_BUTTON_TowerLightsAndBuzzersClear)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON_Onlie)->EnableWindow(0);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(0);
	GetDlgItem(IDC_BTN_EQUIPMENTPARAM)->EnableWindow(0);
	GetDlgItem(IDC_BTN_PLCTEST)->EnableWindow(0);
	
	
}
void CMesTestDlg::IniThread()
{
	CWinThread* MyThread = AfxBeginThread(ThreadStatus, this);
	AfxBeginThread((AFX_THREADPROC)ThreadEx::ThreadListenPLC, NULL);
	AfxBeginThread((AFX_THREADPROC)ThreadEx::ThreadListenMES, NULL);
	AfxBeginThread((AFX_THREADPROC)ThreadEx::ThreadRefreshMes, NULL);
	AfxBeginThread((AFX_THREADPROC)ThreadEx::ThreadSendData, NULL);
	AfxBeginThread((AFX_THREADPROC)ThreadEx::ThreadSendSizeData, NULL);
	AfxBeginThread((AFX_THREADPROC)ThreadEx::ThreadFirstProcess, NULL);
	AfxBeginThread((AFX_THREADPROC)ThreadEx::T_MESONLINEWARN, NULL);
}
UINT CMesTestDlg::ThreadStatus(void *param)
{
	
	CMesTestDlg* Dlg = (CMesTestDlg*)param;
	while (true)
	{
		Dlg->FreshStatus();
		Sleep(1000);
	}
}

void CMesTestDlg::FreshStatus()
{
	CString cstr;
	if (g_Land.PassLevel == 1)
	{
		cstr.Format("用户: 操作员");
		SetDlgItemText(IDC_STA_Status, cstr);
	}
	else if (g_Land.PassLevel == 2)
	{
		cstr.Format("用户: 工程师");
		SetDlgItemText(IDC_STA_Status, cstr);
	}
	else if (g_Land.PassLevel == 3)
	{
		cstr.Format("用户: 管理员");
		SetDlgItemText(IDC_STA_Status, cstr);
	}
	else
	{
		cstr.Format("用户: 未登录");
		SetDlgItemText(IDC_STA_Status, cstr);
	}
	if (g_InfoMes.nHeartBeatStatusFlag == 1)
	{
		m_sta_MESOK.SetBkColor(RGB(0, 255, 0), RGB(0, 0, 0), CStaticExt::Normal);
		m_sta_MESNG.SetBkColor(RGB(255, 255, 255), RGB(0, 0, 0), CStaticExt::Normal);			
	}
	else
	{
		m_sta_MESOK.SetBkColor(RGB(255, 255,255), RGB(0, 0, 0), CStaticExt::Normal);
		m_sta_MESNG.SetBkColor(RGB(255, 0, 0), RGB(0, 0, 0), CStaticExt::Normal);
		
	}
	//static bool bFlag = false;

	//if(g_pPlc)
	//g_pPlc->DoSomething("GETCONNECTION",  NULL,&bFlag);

	if (g_nConnectStatus)
	{
		m_STA_PLCOK.SetBkColor(RGB(0, 255, 0), RGB(0, 0, 0), CStaticExt::Normal);
		m_STA_PLCNG.SetBkColor(RGB(255, 255, 255), RGB(0, 0, 0), CStaticExt::Normal);
	}
	else
	{
		m_STA_PLCOK.SetBkColor(RGB(255, 255, 255), RGB(0, 0, 0), CStaticExt::Normal);
		m_STA_PLCNG.SetBkColor(RGB(255, 0, 0), RGB(0, 0, 0), CStaticExt::Normal);
	}
}




void CMesTestDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码

}
void CMesTestDlg::CreatLog()
{
	CString strDir, strLogDir;
	SYSTEMTIME tSystemtime;
	//GetSystemTime(&tSystemtime);
	GetLocalTime(&tSystemtime);
	strDir.Format(_T("%s\\Log"), GetCurrentAppPath());
	strLogDir.Format(_T("%s\\MESFile\\%04d年%02d月\\%02d日"),
		strDir, tSystemtime.wYear, tSystemtime.wMonth, tSystemtime.wDay);
	CreateMultilevelPath(strLogDir);
	try
	{
		m_strOperateLogFilePath.Format(_T("%s\\OperateLog.txt"), strLogDir);
		if (FALSE == PathFileExists(m_strOperateLogFilePath))
		{
			m_fileOperateLog.Open(m_strOperateLogFilePath, CFile::modeReadWrite | CFile::modeCreate | CFile::typeBinary | CFile::shareDenyNone);
			m_fileOperateLog.Close();
		}


		m_strTimeLogFilePath.Format(_T("%s\\%04d年%02d月%02d日%02d时_MesTimeLog.txt")
			, strLogDir, tSystemtime.wYear, tSystemtime.wMonth, tSystemtime.wDay, tSystemtime.wHour);
		if (FALSE == PathFileExists(m_strTimeLogFilePath))
		{
			m_fileTimeLog.Open(m_strTimeLogFilePath, CFile::modeReadWrite | CFile::modeCreate | CFile::typeBinary | CFile::shareDenyNone);
			m_fileTimeLog.Close();
		}
	}
	catch (...)
	{

	}
	
}
void CMesTestDlg::WriteOperateLog(CString cstr)
{
	SYSTEMTIME CurTime;
	GetLocalTime(&CurTime);
	CString strMessage;
	strMessage.Format(_T("%04d/%02d/%02d-%02d/%02d/%02d.%03d: %s\r\n"), 
		CurTime.wYear, CurTime.wMonth,CurTime.wDay,
		CurTime.wHour, CurTime.wMinute, CurTime.wSecond, CurTime.wMilliseconds, cstr);
	try
	{
		CString  strLogDir;
		strLogDir.Format(_T("%s\\MESFile\\%04d年%02d月\\%02d日"),
			CurTime, CurTime.wYear, CurTime.wMonth, CurTime.wDay);
		m_strTimeLogFilePath.Format(_T("%s\\%04d年%02d月%02d日%02d时_MesTimeLog.txt")
			, strLogDir, CurTime.wYear, CurTime.wMonth, CurTime.wDay, CurTime.wHour);

		if (TRUE == PathFileExists(m_strOperateLogFilePath))
		{
			m_fileOperateLog.Open(m_strOperateLogFilePath, CFile::modeReadWrite | CFile::typeBinary | CFile::shareDenyNone);
		}
		else
		{
			m_fileOperateLog.Open(m_strOperateLogFilePath, CFile::modeCreate | CFile::modeReadWrite | CFile::typeBinary | CFile::shareDenyNone);

		}

		m_fileOperateLog.SeekToEnd();
		m_fileOperateLog.Write(strMessage.GetBuffer(strMessage.GetLength()), strMessage.GetLength());
		m_fileOperateLog.Close();
	}
	catch (...)
	{

	}
}

void CMesTestDlg::WriteTimeLog(CString cstr)
{
	SYSTEMTIME CurTime;
	GetLocalTime(&CurTime);
	CString strMessage;
	strMessage.Format(_T("%04d/%02d/%02d-%02d/%02d/%02d.%03d: %s\r\n"),
		CurTime.wYear, CurTime.wMonth, CurTime.wDay,
		CurTime.wHour, CurTime.wMinute, CurTime.wSecond, CurTime.wMilliseconds, cstr);
	try
	{
		CString strDir, strLogDir;
		strDir.Format(_T("%s\\Log"), GetCurrentAppPath());
		strLogDir.Format(_T("%s\\MESFile\\%04d年%02d月\\%02d日"),
			strDir, CurTime.wYear, CurTime.wMonth, CurTime.wDay);
		m_strTimeLogFilePath.Format(_T("%s\\%04d年%02d月%02d日%02d时_MesTimeLog.txt")
			, strLogDir, CurTime.wYear, CurTime.wMonth, CurTime.wDay, CurTime.wHour);

		if (TRUE == PathFileExists(m_strTimeLogFilePath))
		{
			/*m_fileTimeLog.Open(m_strTimeLogFilePath, CFile::modeReadWrite | CFile::typeBinary | CFile::shareDenyNone);
			m_fileTimeLog.Close();*/
		}
		else
		{
			CMesTestDlg::CreatLog();
			m_fileTimeLog.Open(m_strTimeLogFilePath, CFile::modeCreate | CFile::modeReadWrite | CFile::typeBinary | CFile::shareDenyNone);
			m_fileTimeLog.Close();

		}
		CStdioFile recordFile;
		BOOL bRet = recordFile.Open(m_strTimeLogFilePath, CFile::modeReadWrite | CFile::typeBinary | CFile::shareDenyNone | CFile::modeCreate | CFile::modeNoTruncate);
		if (!bRet)
		{
			CString cstrException;
			cstrException.Format(_T("打开文件异常"));
			((CMesTestDlg*)g_pMainDialog)->WriteTimeLog(cstrException);

		}
		else
		{
			recordFile.SeekToEnd();
			recordFile.Write(strMessage.GetBuffer(strMessage.GetLength()), strMessage.GetLength());
			strMessage.ReleaseBuffer();
			recordFile.Close();
		}
		
	}
	catch (...)
	{

	}
}


void CMesTestDlg::InitLogList()
{
	m_List_Log.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER);

	m_List_Log.SetColorHeader(TRUE);
	m_List_Log.SetHeaderBKColor(255, 255, 255, 0);
	CString cstrTitle;
	cstrTitle.Format(_T("序号"));
	m_List_Log.InsertColumn(0, cstrTitle, LVCFMT_LEFT, 50);
	cstrTitle.Format(_T("日志信息"));
	m_List_Log.InsertColumn(1, cstrTitle, LVCFMT_LEFT, 1500);
	for (int i = 0; i < 100; i++)
	{
		m_List_Log.InsertItem(i, "");

	}
}

void CMesTestDlg::UpdateLogList(CString cstrMsg)
{
	SYSTEMTIME CurSysTime;
	CString  Stime;
	GetLocalTime(&CurSysTime);
	Stime.Format("%02d-%02d-%02d-%02d-%02d-%02d", CurSysTime.wYear, CurSysTime.wMonth, CurSysTime.wDay, CurSysTime.wHour, CurSysTime.wMinute, CurSysTime.wSecond);
	cstrMsg.Format("%s %s", Stime, cstrMsg);
	CLockHelper tLock(&m_Log);
	m_listLog.push_front(cstrMsg);

	while (m_listLog.size() >= 20)
	{
		m_listLog.pop_back();
	}

	std::list<CString>::iterator  itr = m_listLog.begin();
	for (int i = 0; itr != m_listLog.end() && i <20; itr++, i++)
	{
		m_List_Log.SetItemText(i, 1, *itr);
		cstrMsg.Format("%d", i + 1);
		m_List_Log.SetItemText(i, 0, cstrMsg);
	}
	
}
void CMesTestDlg::SendMES_PRODUCT_PARA_COLLOCT_UNIVERSAL()
{

}
void CMesTestDlg::SendMES_RAW_MATERIAL_INPUT_FIRST()
{

}

void CMesTestDlg::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码

}


void CMesTestDlg::OnBnClickedBtnSaveparam()
{
	// TODO: 在此添加控件通知处理程序代码
	if (g_Land.PassLevel >= 1)
	{

		UpdateData(TRUE);
		g_bUseMes = m_CHK_bUseMes;
		g_bUsePlc = m_CHK_bUsePLC;

        //PLC地址
		WritePrivateProfileInt("PlcCom", "Use", g_bUsePlc , g_FilePathPLC);
		WritePrivateProfileInt("Address", "TotalCount", g_PLCAddress.TotalCount , g_FilePathPLC);
		WritePrivateProfileInt("Address", "NGCount", g_PLCAddress.NGCount , g_FilePathPLC);
		WritePrivateProfileInt("Address", "OKCount", g_PLCAddress.OKCount , g_FilePathPLC);
		WritePrivateProfileInt("Address", "OKBoxDequeueFlagAddr", g_PLCAddress.OKBoxDequeueFlagAddr , g_FilePathPLC);

		WritePrivateProfileInt("Address", "MisMatch", g_PLCAddress.MisMatch , g_FilePathPLC);

		WritePrivateProfileInt("Address", "FilmThickness", g_PLCAddress.FilmThickness , g_FilePathPLC);
		WritePrivateProfileInt("Address", "CutterLife", g_PLCAddress.CutterLife , g_FilePathPLC);
		WritePrivateProfileInt("Address", "MouldLife", g_PLCAddress.MouldLife , g_FilePathPLC);
		WritePrivateProfileInt("Address", "UnreelingSpeedRadio", g_PLCAddress.UnreelingSpeedRadio , g_FilePathPLC);
		WritePrivateProfileInt("Address", "DrawPullRatio", g_PLCAddress.DrawPullRatio , g_FilePathPLC);
		WritePrivateProfileInt("Address", "SetBoxQuanlity", g_PLCAddress.SetBoxQuanlity , g_FilePathPLC);
		WritePrivateProfileInt("Address", "FilmMouldCutSetLength", g_PLCAddress.FilmMouldCutSetLength , g_FilePathPLC);
		WritePrivateProfileInt("Address", "MouldCutSpeed", g_PLCAddress.MouldCutSpeed , g_FilePathPLC);
		WritePrivateProfileInt("Address", "AlarmCodeAddr", g_PLCAddress.AlarmCodeAddr , g_FilePathPLC);
		WritePrivateProfileInt("Address", "AlarmCodeFlagAddr", g_PLCAddress.AlarmCodeFlagAddr , g_FilePathPLC);

		WritePrivateProfileInt("Address", "CommandDownloadAddr", g_PLCAddress.CommandDownloadAddr , g_FilePathPLC);

		WritePrivateProfileInt("Address", "DeviceStatusAddr", g_PLCAddress.DeviceStatusAddr , g_FilePathPLC);
		WritePrivateProfileInt("Address", "BatchAddr", g_PLCAddress.BatchAddr , g_FilePathPLC);

		WritePrivateProfileInt("Address", "LengthOffsetAddr", g_PLCAddress.LengthOffsetAddr , g_FilePathPLC);
		WritePrivateProfileInt("Address", "DownloadParamModifyAddr", g_PLCAddress.DownloadParamModifyAddr, g_FilePathPLC);



		//MES数据
		WritePrivateProfileInt("MesCom", "Use", g_bUseMes, g_FilePathLog);
		WritePrivateProfileString("EquipmentInfo", "IP", g_InfoMes.IP, g_FilePathLog);
		WritePrivateProfileInt("EquipmentInfo", "Port", g_InfoMes.Port, g_FilePathLog);
		WritePrivateProfileDouble("EquipmentInfo", "dTimeOut", g_InfoMes.dTimeOut , g_FilePathLog);
		WritePrivateProfileInt("EquipmentInfo", "bServer", g_InfoMes.bServer, g_FilePathLog);
		WritePrivateProfileInt("EquipmentInfo", "bShiledHeartBeat", g_InfoMes.bShiledHeartBeat, g_FilePathLog);

		WritePrivateProfileString("EquipmentInfo", "factoryNo", g_InfoMes.factoryNo, g_FilePathLog);
		WritePrivateProfileString("EquipmentInfo", "machineNo", g_InfoMes.machineNo, g_FilePathLog);

		WritePrivateProfileString("EquipmentInfo", "MesUserName", g_InfoMes.MesUserName , g_FilePathLog);
		WritePrivateProfileString("EquipmentInfo", "MesUserPassWord", g_InfoMes.MesUserPassWord, g_FilePathLog);
		WritePrivateProfileString("EquipmentInfo", "EventUser",  g_InfoMes.EventUser, g_FilePathLog);
		
		WritePrivateProfileInt("EquipmentInfo", "nHeartBeatSpanTime", g_InfoMes.nHeartBeatSpanTime, g_FilePathLog);
		

		WritePrivateProfileString("EquipmentInfo", "Batch", g_InfoMes.cstrBatch,g_FilePathLog);
		WritePrivateProfileInt("EquipmentInfo", "nTimeJianGe", g_InfoMes.nTimeJianGe, g_FilePathLog);



		//系统数据
		WritePrivateProfileString("Land", "Use1",  g_Land.Use1, g_SystemPathPLC);
		WritePrivateProfileString("Land", "PassWord1",  g_Land.PassWord1, g_SystemPathPLC);
		WritePrivateProfileString("Land", "Use2",  g_Land.Use2, g_SystemPathPLC);
		WritePrivateProfileString("Land", "PassWord2", g_Land.PassWord2, g_SystemPathPLC);
		WritePrivateProfileString("Land", "Use3",  g_Land.Use3, g_SystemPathPLC);
		WritePrivateProfileString("Land", "PassWord3",  g_Land.PassWord3, g_SystemPathPLC);

		
		
		AfxMessageBox("保存成功");
	}
	else
	{
		AfxMessageBox("请登录更高等级权限");
		return;
	}
}

void CMesTestDlg::SendMES_QUIPMENT_DATA_COLLECT()
{
}


void CMesTestDlg::OnBnClickedBtnEquipmentparam()
{
	// TODO: 在此添加控件通知处理程序代码

}


void CMesTestDlg::SendMES_USER_RIGHT_VERIFY()
{

}



void CMesTestDlg::OnBnClickedBtnMesparam()
{
	// TODO: 在此添加控件通知处理程序代码
	CParamSetDlg m_tParamSet;
	m_tParamSet.DoModal();
}

void CMesTestDlg::GetFilePath(CString strMapPath,  CString &path)
{
	CFileFind finder;

	CString strFilePath;
	
	strFilePath.Format(_T("%s"), strMapPath);

	if (strFilePath == _T(""))
	{
		return;
	}
	else
	{
		if (strFilePath.Right(1) != _T(""))
		{
			strFilePath += _T("\\");
		}
		strFilePath += _T("*.*");
	}

	BOOL isNotEmpty = finder.FindFile(strFilePath);//总文件夹，开始遍历 
	while (isNotEmpty)
	{
		isNotEmpty = finder.FindNextFile();//查找文件 
		CString filename = finder.GetFilePath();//获取文件的路径，可能是文件夹，可能是文件 
		if (!(finder.IsDirectory()))
		{
			//如果是jpg.csv文件则加入文件列表

			CString strFileName;

			CString strFileExtendName;

			strFileName = filename.Right(filename.GetLength() - filename.ReverseFind('\\') - 1);

			strFileExtendName = strFileName.Right(strFileName.GetLength() - strFileName.Find('.') - 1);

			if (strFileExtendName == _T("ini"))
			{
				
				CString strFileName1, strFileName2;
				strFileName1 = filename;
				strFileName2 = strFileName;				
				strFileName1.Replace(strFileName, strFileName2);
				/*::CopyFile(filename, strFileName1, TRUE);
				::DeleteFileA(filename);*/
				strFileName1.Replace("\\", "/");
				filename.Replace("\\", "/");
			
				path = strFileName1;
				int status=GetFileAttributes(path);
				if (status ==128)
				{
					if (path != g_cstrErrorFilePath)
					{
						g_cstrErrorFilePath = path;
					}
					else
					{
						SetFileAttributes(path, FILE_ATTRIBUTE_NORMAL);   //设置文件为一般模式
						HTuple hv_path;
						hv_path = (HTuple)path;
						DeleteFile(hv_path);
						g_cstrErrorFilePath = "";
					}
					path = "";

				}
				return;
			}
			else
			{													//递归遍历用户文件夹，跳过非用户文件夹
				
			}
		}
		else
		{													//递归遍历用户文件夹，跳过非用户文件夹
			
		}

	}
	path = "";
	return;
}